var web = web || {};
web.ItemTimer = Class.create(); 
web.ItemTimer.prototype = {
	initialize: function(options) {
		var _this = this;
		this.settings = $.extend({
			delay: 100,
			showZero: true,
			msFixed: 3,
			onShow: null
		}, options);
		
		this.timer = null;
		this.localTime = 0;
		this.serverTime = 0;
		this.targetTime = 0;
		this.dayMs = 24 * 60 * 60 * 1000;
		this.hourMs = 60 * 60 * 1000;
		this.MinMs = 60 * 1000;
		this.secMs = 1000;
		this.leftMs = 0;
		this.isShowCallback = $.type(this.settings.onShow) == 'function';
		this.has = {};
		
		this.element = null;
		this.day = null;
		this.hour = null;
		this.min = null;
		this.sec = null;
		this.ms = null;
	},
	init: function(el) {
		var _this = this;
		this.element = getJqueryElement(el);
		if (null == this.element) return;
		
		this.serverTime = this.element.attr('data-servertime');
		this.targetTime = this.element.attr('data-targettime');
		if (!this.serverTime || !this.targetTime) return;
		this.serverTime = Number(this.serverTime);
		this.targetTime = Number(this.targetTime);
		if (isNaN(this.serverTime) || isNaN(this.targetTime)) return;
		
		if (!this.isShowCallback) {
			this.day = this.element.find('.it-day');
			this.hour = this.element.find('.it-hour');
			this.min = this.element.find('.it-min');
			this.sec = this.element.find('.it-sec');
			this.ms = this.element.find('.it-ms');
			
			this.has.day = (this.day.size() == 1);
			this.has.hour = (this.hour.size() == 1);
			this.has.min = (this.min.size() == 1);
			this.has.sec = (this.sec.size() == 1);
			this.has.ms = (this.ms.size() == 1);
		}
		
		this.timer = window.setInterval(function(){
			_this._count();
		}, this.settings.delay);
	},
	_count: function() {
		var localNow = new Date().getTime();
		if (0 == this.localTime) {
			this.localTime = localNow;
		}
		
		var serverNow = this._getServerNowTime(localNow);
		if (serverNow > this.targetTime) {
			window.clearInterval(this.timer);
		}
		
		
		this.leftMs = this.targetTime - serverNow;
		var day = parseInt(this.leftMs / this.dayMs);
		
		this.leftMs = this.leftMs - (this.dayMs * day);
		var hour = parseInt(this.leftMs / this.hourMs);
		
		this.leftMs = this.leftMs - (this.hourMs * hour);
		var min = parseInt(this.leftMs / this.MinMs);
		
		this.leftMs = this.leftMs - (this.MinMs * min);
		var sec = parseInt(this.leftMs / this.secMs);
		
		var ms = this.leftMs - (this.secMs * sec);
		
		this._showTime(day, hour, min, sec, ms);
	},
	_showTime: function(day, hour, min, sec, ms) {
		var _mi = this._format(min);
		var _se = this._format(sec);
		var _ms = this._formatMs(ms)
		
		if (this.isShowCallback) {
			this.settings.onShow.apply(this, day, hour, _mi, _se, _ms);
		} else {
			if (this.has.day) this.day.html(day);
			if (this.has.hour) this.hour.html(hour);
			if (this.has.min) this.min.html(_mi);
			if (this.has.sec) this.sec.html(_se);
			if (this.has.ms) this.ms.html(_ms);
		}
	},
	_format: function(val) {
		if (val < 10) return '0'+ val;
		else return val;
	},
	_formatMs: function(val) {
		var v = val;
		if (this.settings.msFixed == 1) {
			v = parseInt(v / 100);
		} else if (this.settings.msFixed == 2) {
			v = this._format(parseInt(v / 10))
		} else {
			if (val < 100) v = '0'+ val;
			else if (val < 10) v = '00'+ val;
		}
		return v;
	},
	_getServerNowTime: function(now) {
		return this.serverTime + (now - this.localTime);
	}
};

(function($){  
	$.fn.itemTimer = function(options){ 
		return this.each(function(){
			new web.ItemTimer(options).init($(this));
		});
	}
})(jQuery); 

